package com.distribute.framework.core.sql;

import com.distribute.framework.core.web.helper.Utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import com.distribute.framework.core.sql.datasource.Lazy3qDataSource;

import com.distribute.framework.core.web.util.Lazy3qLogger;

public class Lazy3qSQL
{
	public static Boolean bInit=false;
	
	private static Map<String, Lazy3qDaoSupport> daos = new HashMap<String, Lazy3qDaoSupport>();//DAO	
	
	private Lazy3qSQL(){}
	
	
	/**
	 * 初始化Lazy3qSQL，加载Lazy3qSQL数据源配置文件
	 * classpath下的lazy3q_sql.xml或config.properties文件
	 */
	public static void InitLazy3qSQL()
	{
		InitLazy3qSQLFromProperties();
	}
	
	
	private static void InitLazy3qSQLFromProperties()
	{
		
		String strShowSql=Utils.config("lazy3q.shwoSql");
		if(strShowSql==null || strShowSql.equals(""))
			strShowSql="true";
		String strDebug=Utils.config("lazy3q.debug");
		if(strDebug==null || strDebug.equals(""))
			strDebug="true";
		
		Lazy3qLogger.bShowSql=Boolean.valueOf(strShowSql)||strShowSql.equals("1");
				
		bInit=true;

		//Lazy3qLogger.info("加载Lazy3qSQL数据源配置文件成功！");
	}

	public static Boolean empty(String string)
	{
		if(string==null || string.trim().equals(""))
			return true;
		return false;		
	}
		
	public static void createDataSource(String sourceName){	
		DataSource dataSource = null;
		try{
			dataSource = Utils.getBean(Utils.forMat(sourceName)+"DataSource");
		}catch(Exception ex){}catch(Error ex){}
		if(dataSource==null){
			String sDrive = Utils.or(Utils.config(sourceName+".driver"), Utils.config(sourceName+".driver-class-name"));
			String sUrl = Utils.config(sourceName+".url");
			String sUsername = Utils.config(sourceName+".username");
			String sPassword = Utils.config(sourceName+".password");
			
			if(empty(sDrive)){
				Lazy3qLogger.error("找不到属性"+sourceName+".driver");
				return;
			}
			if(empty(sUrl)){
				Lazy3qLogger.error("找不到属性"+sourceName+".url");
				return;
			}
			if(empty(sUsername)){
				Lazy3qLogger.error("找不到属性"+sourceName+".username");
				return;
			}
			if(empty(sPassword)){
				Lazy3qLogger.error("找不到属性"+sourceName+".password");
				return;
			}
			
			
			Integer iMax=10;
			try{
				iMax=Integer.parseInt(Utils.config(sourceName+".maxs"));
			}catch(Exception ex){
				iMax=10;
			}
	
			Integer iIdle=1800;
			try{
				iIdle=Integer.parseInt(Utils.config(sourceName+".idle"));
			}catch(Exception ex){
				iIdle=1800;
			}
			
			Integer iTimeout=3;
			try{
				iTimeout = Integer.parseInt(Utils.config(sourceName+".timeout"));
			}catch(Exception ex){
				iTimeout=3;
			}
			dataSource=new Lazy3qDataSource(sUrl, sDrive, sUsername,sPassword, iMax, iTimeout,iIdle);
		}
		Lazy3qDaoSupport dao=new Lazy3qDaoSupport(dataSource);
		daos.put(sourceName, dao);
	}
	
	/**
	 * 根据连接池名取DAO
	 * @param sourceName
	 * @return 
	 */
	public static Lazy3qDaoSupport GetDao(String sourceName)
	{
		if(!bInit)
			InitLazy3qSQL();
		
		Lazy3qDaoSupport dao = daos.get(sourceName);
		if(dao==null)
			createDataSource(sourceName);
		
		return daos.get(sourceName);
	}
	
	/**
	 * 添加DAO实例
	 * @param sourceName
	 * @param dao
	 */
	public static void PutDao(String sourceName,Lazy3qDaoSupport dao)
	{
		daos.put(sourceName,dao);
	}
		
	public static void main(String...argv)
	{
		try{
			Lazy3qDaoSupport dao = Lazy3qSQL.GetDao("qxton");
			List<Map> list=dao.executeQuery("select * from srv_user");
			Map map = list.get(0);
			System.out.println(map.get("su_rEal_name"));
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}